package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.exception.QueryException;
import com.mrd.gtimp.business.entity.GoodsCode;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * <p>
 * application name: gtimp
 * <p>
 * copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */
@Service
public class GoodsCodeBiz extends BaseBiz<GoodsCode> {
    private static final Logger logger = LoggerFactory.getLogger(GoodsCodeBiz.class);

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("goodsCodeDao")
    public void setBaseRepository(BaseRepository<GoodsCode, String> baseRepository) {
        super.baseRepository = baseRepository;
    }

    /**
     * 查询商品编号
     */
    public Page<GoodsCode> findGoodCode(String keyWord, Pageable pageable) throws QueryException {
        Map<String, Object> map = new HashMap<>();
        String sql = "select goods.code_t,goods.code_s,goods.unit_1,goods.g_name,goods.unit_2,goods.goods_mode from cdams_qp_goods_code goods ";
        if (StringUtils.isNotBlank(keyWord)) {
            sql += "  where goods.code_t like :keyword ";
            map.put("keyword", "%" + keyWord.trim() + "%");
        }
        List<Object> objects = findObjectBySql(sql, pageable.getPageNumber(), pageable
                .getPageSize(), map);
        return new PageImpl<>(sreult(objects));
    }

    public GoodsCode findByCodet(String codeT) throws QueryException {
        Map<String, Object> map = new HashMap<>();
        String sql = "select goods.code_t,goods.code_s,goods.unit_1,goods.g_name,goods.unit_2,goods.goods_mode from cdams_qp_goods_code goods where 1 = 1";
        if (StringUtils.isNotBlank(codeT)) {
            sql += "  and goods.code_t like :codet ";
            map.put("codet", codeT.trim());
        }
        List<Object> objects = findObjectBySql(sql, 0, 1, map);
        if (CollectionUtils.isNotEmpty(objects)) {
            return sreult(objects).get(0);
        }
        return null;
    }

    /**
     * 查询商品编号 （2.0一线通）
     */
    public Page<GoodsCode> findGoodOfCode(String keyWord) throws QueryException {
        PageRequest pageRequest = new PageRequest(0, 1000);
        return findGoodCode(keyWord, pageRequest);
    }

    // 查询商品编号 (返回值)
    private List<GoodsCode> sreult(List<Object> objects) {
        List<GoodsCode> goodsCodes = new ArrayList<>();
        for (Object object : objects) {
            GoodsCode goodsCodes1 = new GoodsCode();
            Object[] obj = (Object[]) object;
            goodsCodes1.setCODE_S(obj[1] == null ? "" : obj[1].toString());
            goodsCodes1.setCODE_T(obj[0] == null ? "" : obj[0].toString());
            goodsCodes1.setUNIT_1(obj[2] == null ? "" : obj[2].toString());
            goodsCodes1.setG_NAME(obj[3] == null ? "" : obj[3].toString());
            String unit2 = obj[4] == null ? "" : obj[4].toString();
            if (StringUtils.isNotEmpty(unit2)) {
                try {
                    unit2 = String.format("%03d", Integer.parseInt(unit2));
                } catch (Exception e) {
                    logger.warn("第二单位解析错误!code_s:{},code_t:{},g_name:{},unit2:{}", goodsCodes1
                            .getCODE_S(), goodsCodes1.getCODE_T(), goodsCodes1.getG_NAME(), obj[4]);
                    unit2 = "";
                }
            }
            goodsCodes1.setUNIT_2(unit2);
            if (obj[5] != null) {
                goodsCodes1.setGOODS_MODE(obj[5].toString());
            }
            goodsCodes.add(goodsCodes1);
        }
        return goodsCodes;
    }

}
